package com.codeblue.tools.codeGenerator;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.com.qingz.core.model.FiledComment;


public class EntityDesc {
	
	private String lowerName;  // user
	private String className;   //User 
	private String packageName;    //com.wsq.system.entity.User
	private String cnName ; //用户
	private String upperName  ;//USER        
	private Class<?> entity;
	
	private List<String> fieldList ;
	private List<String> fieldTokenList ;
	
	private List<Map<String,Object>> valuedFields ; //适用于  x = #{x}
	private List<Map<String,Object>> valueedFieldsExt ; //适用于  x = ${x}
	private String id; //主键名
	private String upperId;
	
	private String idToken;
	

	
	public String getUpperId() {
		String s= this.getId() ;
		String s1 = s.substring(0, 1).toUpperCase();
		String s2 = s.substring(1, s.length());
		return s1+s2;
	}
	public void setUpperId(String upperId) {
		this.upperId = upperId;
	}
	public String getUpperName() {
		return this.getClassName().toUpperCase();
	}
	public void setUpperName(String upperName) {
		this.upperName = upperName;
	}
	public String getIdToken() {
		return "#{"+id+"}";
	}
	public EntityDesc(Class<?> entity,String cnName){
		this.entity  = entity;
		this.cnName = cnName;
		getFieldList();
		
	}
	public String getLowCaseName(String name){
		String s1 = name.substring(0, 1).toLowerCase();
		String s2 = name.substring(1, name.length());
		return s1+s2;
	}
	
	public List<String> getFieldList() {
		 
		fieldList = new ArrayList<String>(20); 
		fieldTokenList = new ArrayList<String>(20); 
		valuedFields= new   ArrayList<Map<String,Object>>(20);
		valueedFieldsExt =   new   ArrayList<Map<String,Object>>(20);
	    Field thisFields[] = entity.getDeclaredFields();
	    Class<?> superClass = entity.getSuperclass();
	    Field[] superFields= superClass.getDeclaredFields();
	  
	    List<Field> fields = new ArrayList<Field>();
	    
	    fields.addAll(Arrays.asList(thisFields));
	    if(!"com.wsq.common.dto.Condition".equals(superClass.getName())){
	    	 fields.addAll(Arrays.asList(superFields));
	    }
		 for (Field field : fields) {
		
			 Annotation ans []= field.getAnnotations();
			for (Annotation annotation : ans) {
				if (annotation instanceof FiledComment) {
					FiledComment an = (FiledComment) annotation;
					if(an.isId()){
						this.id = field.getName();
					}
				}
			}
			  
			 Map<String,Object> fieldDesc = new HashMap<String,Object>();
			 fieldDesc.put("value", "#{"+ field.getName() +"}") ;
			 fieldDesc.put("name",  field.getName() ) ;
			 valuedFields.add( fieldDesc) ;
			 
			 Map<String,Object> fieldDescExt = new HashMap<String,Object>();
			 fieldDescExt.put("value", "${"+ field.getName() +"}") ;
			 fieldDescExt.put("name",  field.getName() ) ;
			 valueedFieldsExt.add(fieldDescExt) ;
			 
			 fieldList.add( field.getName()) ;
			 fieldTokenList.add( "#{"+ field.getName() +"}") ;
			 
			 
	 	 }
		return fieldList;
	}

	
	
	public String getLowerName() {
		return this.entity.getSimpleName().toLowerCase();
//		return getLowCaseName(this.entity.getSimpleName());
	}

	public String getClassName() {
		return this.entity.getSimpleName();
	}

	public String getPackageName() {
		return this.entity.getName();
	}

	public String getCnName() {
		return cnName;
	}

	public void setCnName(String cnName) {
		this.cnName = cnName;
	}
	public Class<?> getEntity() {
		return entity;
	}
	public void setEntity(Class<?> entity) {
		this.entity = entity;
	}
	public List<String> getFieldTokenList() {
		return fieldTokenList;
	}
	public void setFieldTokenList(List<String> fieldTokenList) {
		this.fieldTokenList = fieldTokenList;
	}
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public void setLowerName(String lowerName) {
		this.lowerName = lowerName;
	}
	public void setClassName(String className) {
		this.className = className;
	}
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}
	public void setFieldList(List<String> fieldList) {
		this.fieldList = fieldList;
	}
	public List<Map<String, Object>> getValuedFields() {
		return valuedFields;
	}
	public void setValuedFields(List<Map<String, Object>> valuedFields) {
		this.valuedFields = valuedFields;
	}
	public void setIdToken(String idToken) {
		this.idToken = idToken;
	}
	public List<Map<String, Object>> getValueedFieldsExt() {
		return valueedFieldsExt;
	}
	public void setValueedFieldsExt(List<Map<String, Object>> valueedFieldsExt) {
		this.valueedFieldsExt = valueedFieldsExt;
	}
	
	
}
